﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Reflection;


namespace 打开
{

    public partial class Form1 : Form
    {

        #region 变量声明

        //声明提示框
        MessegeForm msg = new MessegeForm();

        //声明按钮
        ButtonQuick[] anniu = new ButtonQuick[15];

        #endregion

        /*Form1初始化*/
        public Form1()
        {

            InitializeComponent();

            data_load();
            button_load();

            打开Ver10ToolStripMenuItem.Text = 托盘图标.Text = Text = "打开 C#版 Ver" + Assembly.GetExecutingAssembly().GetName().Version;

        }     

        #region Form1事件

        protected override void OnLoad(EventArgs e)
        {
            //加载窗口隐藏
            HideForm();

            //窗口位置为上次窗口关闭的位置
            Location = DataManager.startLocation;

            new Updater("XultimateX", "quicklyopen");
        }

        protected override void OnDragEnter(DragEventArgs drgevent)
        {
            显示ToolStripMenuItem_Click(null, EventArgs.Empty);
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            //data.DataWrite( data.DataPack(Location, ButtonQuick.ButtonData));
            DataManager.DataWriter();
            托盘图标.Icon = null;
        }

        protected override void OnClosed(EventArgs e)
        {

            try
            {
                Application.Exit();
            }
            catch (Win32Exception)
            {
                Thread.Sleep(500);
                Environment.Exit(0);
            }

        }

        protected override void OnMouseWheel(MouseEventArgs e)
        {

            int sign = 1;

            if (e.Delta > 0)
            {
                sign = -1;
            }

            try { hScrollBar1.Value = hScrollBar1.Value + sign; }
            catch (ArgumentOutOfRangeException)
            {
                return;
            }
            ButtonQuick.Slide(sign, hScrollBar1.Value);

            Console.WriteLine("鼠标滚轮 " + hScrollBar1.Value);

        }

        #endregion


        /// <summary>数据加载函数</summary>
        private void data_load()
        {
            new DataManager();

            Location = DataManager.startLocation;

        }

        /// <summary>按钮加载函数</summary>
        private void button_load()
        {

            int k = 0, j, i;
            for (j = 0; j < 3; j++)
            {
                for (i = 0; i < 5; i++)
                {
                    anniu[k] = new ButtonQuick(
                                                    new Point(35 + 50 * i, 20 + 50 * j),
                                                    new Size(40, 40),
                                                    DataManager.buttonData[0, k]
                                               );
                    Controls.Add(anniu[k]);
                    k++;
                }
            }

        }

        /// <summary>滑条滑动事件</summary>
        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            int ChangedValue = e.OldValue - e.NewValue;

            if (Math.Abs(ChangedValue) == 1)
            {
                if (ChangedValue > 0)
                {
                    ButtonQuick.Slide(ButtonQuick.SliderType.Reduce, --hScrollBar1.Value);
                }
                else
                {
                    ButtonQuick.Slide(ButtonQuick.SliderType.Add, ++hScrollBar1.Value);
                }
            }
            Console.WriteLine(ChangedValue);
        }

    }

}
